{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from ltp import LTP\n",
    "from pprint import pprint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def parse_dep(cws, dep_head, dep_label):\n",
    "    result = []\n",
    "    for idx in range(len(cws)+1):\n",
    "        if idx == 0:\n",
    "            result.append({\n",
    "                \"name\": 'root',\n",
    "                \"to\": [],\n",
    "                \"in\": []\n",
    "            })\n",
    "        else:\n",
    "            result.append({\n",
    "                \"name\": cws[idx - 1],\n",
    "                \"to\": [],\n",
    "                \"in\": []\n",
    "            })\n",
    "\n",
    "    for cws_idx, in_idx in enumerate(dep_head):\n",
    "        result[cws_idx + 1][\"in\"].append({\n",
    "            \"idx\": in_idx,\n",
    "            \"name\": cws[in_idx - 1],\n",
    "            \"label\": dep_label[cws_idx]\n",
    "        })\n",
    "        result[in_idx][\"to\"].append({\n",
    "            \"idx\": cws_idx + 1,\n",
    "            \"name\": cws[cws_idx],\n",
    "            \"label\": dep_label[cws_idx]\n",
    "        })\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading weights from local directory\n",
      "芋头中含有丰富的钾，它能排出体内多余盐分，具有预防高血压的功效。\n",
      "[('芋头', '含有', '钾')]\n",
      "**************************************************\n",
      "牡蛎是锌的重要供给源。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "全谷、杂粮豆类中含有较高水平的植酸，不少人担心食用过多会影响钙、铁等的吸收，发生缺钙、贫血等不良问题。\n",
      "[('全谷', '含有', '植酸')]\n",
      "**************************************************\n",
      "牛蒡中膳食纤维含量很高，其中的菊糖能增加肠道益生菌的数量，刺激肠道蠕动，调整肠道功能。\n",
      "[]\n",
      "**************************************************\n",
      "西兰花营养丰富，富含蛋白质、碳水化合物、脂肪、矿物质、维生素C和胡萝卜素等，可有效调节人体酸碱平衡，有助于骨骼健康。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "海带含有大量的不饱和脂肪酸，能清除附着在血管壁上的过多胆固醇；海带含有的昆布素等多糖可降低血清胆固醇和甘油三酯的含量。\n",
      "[('海带', '含有', '不饱和脂肪酸')]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "增加全谷物的消费，减少精白米面的摄入；在保证充足蔬菜摄入的前提下，强调增加深色蔬菜的消费比例；增加新鲜水果的摄入；增加富含优质蛋白质的豆类及其制品摄入。\n",
      "[]\n",
      "**************************************************\n",
      "牛磺酸还能促进胆汁酸的分泌，乳化食物中的脂防，使人体更容易吸收，具有减肥功效。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "黄豆及其制品所提供的优质蛋白质可以媲美动物性蛋白质，应成为素食人群获取蛋白质的主要途径。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "长时间煮炖减少胆固醇。猪肉经长时间炖、煮后，不饱和脂肪酸增加，胆固醇含量会大大降低，尤其适合血脂高、血压高的人。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "另外，黄豆还含有卵磷脂、丰富的维生素及其他矿旷物质，特别适合脑力工作者。\n",
      "[('黄豆', '含有', '维生素'), ('黄豆', '含有', '卵磷脂')]\n",
      "**************************************************\n",
      "**************************************************\n",
      "植物性食物中铁的吸收率较低，还容易受到植酸、草酸等因素的干扰。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "在血糖控制好的情况下，可适当吃苹果，苹果中的维生素C、钾、膳食纤维能促进脂肪分解，预防肥胖，还能预防糖尿病并发高血压等并发症。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "比如，不可溶性膳食纤维（可溶性和不可溶性膳食纤维对延缓餐后的血糖升高均有效)主要存在于谷物的表皮中。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "芝麻中含有不饱和脂肪酸的亚油酸。芝麻素是芝麻中的特有成分，它是芝麻准木质素的一种，有着很强的抗氧化作用。\n",
      "[('芝麻', '含有', '亚油酸'), ('芝麻', '含有', '不饱和脂肪酸')]\n",
      "**************************************************\n",
      "青椒的绿色是因为青椒中含有一种名叫叶绿素的天然色素。\n",
      "[]\n",
      "**************************************************\n",
      "针眼患者需要食用鳗鱼和胡萝卜等食物，摄取能够保护眼晴健康的维生素A\n",
      "[]\n",
      "**************************************************\n",
      "润肠通便、排毒，宜生吃。生吃胡萝卜容易让其中的水溶性维生素和矿物质被人体吸收，所以喝鲜榨的胡萝卜汁，可以起到润肠通便、排毒的作用。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "白萝卜辣味成分中的异硫氰酸盐也具有防止血栓以及解毒等功效。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "眼睛疲劳需要补充“眼睛的维生素”一维生素A。\n",
      "[]\n",
      "**************************************************\n",
      "代表性的例子是被称为“第六营养素”的膳食纤维。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "豇豆所含的B族维生素能维持正常的消化腺分泌和肠胃蠕动功能，可帮助消化，防治慢性胃炎。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "特别是农村居民的膳食结构得到较大的改善，碳水化合物的供能比从1992年的70.1%下降到2015年的55.3%，动物性食物提供的蛋白质从1992年的12.4%提高到2015年的31.4%，城乡差距逐渐缩小。\n",
      "[]\n",
      "**************************************************\n",
      "裙带菜中含有膳食纤维，具有改善便秘和降血压的作用。\n",
      "[('裙带菜', '含有', '膳食纤维')]\n",
      "**************************************************\n",
      "牛奶当中含有蛋白质、钙、铁、维生素A、维生素B2等多种营养素。\n",
      "[('牛奶', '含有', '维生素B2'),\n",
      " ('牛奶', '含有', '维生素A'),\n",
      " ('牛奶', '含有', '铁'),\n",
      " ('牛奶', '含有', '蛋白质'),\n",
      " ('牛奶', '含有', '钙')]\n",
      "**************************************************\n",
      "钾在人体中的作用：与细胞外液的钠合作，调节渗透压；与细胞的新陈代谢有关，参与热量代谢，一定浓度的钾，可维持细胞内一些酶的活性；维持神经肌肉系统的正常功能，维持心肌正常功能。\n",
      "[]\n",
      "**************************************************\n",
      "鲑鱼中维生素D含量丰富，有助于人体对钙质的吸收，搭配牛奶也能提高人体对牛奶的吸收率。\n",
      "[]\n",
      "**************************************************\n",
      "黑芝麻及羊栖菜等食物中富含的神经酰胺能保持皮肤滋润，有效缓解皮肤干燥。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "弛缓性便秘由于肠道蠕动弱、粪便移动速度慢而引起，应补充不溶性膳食纤维含量多的食物，如全麦、谷物、豆类、根茎类、果皮、胡萝卜、柑橘等。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "血脂异常患者应该增加膳食纤维的摄入。增加粗粮、豆类、薯类等富含膳食纤维的食物的摄入，可减少对食物中脂肪的吸收。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "罗勒中含有丰富的β-胡萝卜素，具有很强的抗氧化能力，能防止衰老。\n",
      "[('罗勒', '含有', 'β-胡萝卜素')]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "维生素A推荐摄入量：男性每天800微克；女性每天700微克。\n",
      "[]\n",
      "**************************************************\n",
      "水果中含有碳水化合物、膳食纤维、维生素C、钾、镁、植物化合物等营养素\n",
      "[('水果', '含有', '膳食纤维'),\n",
      " ('水果', '含有', '维生素C'),\n",
      " ('水果', '含有', '镁'),\n",
      " ('水果', '含有', '碳水化合物'),\n",
      " ('水果', '含有', '钾')]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "β-胡萝卜素富含于胡萝卜、菠菜等蔬菜中。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "黄瓜含有维生素C,可促进糖代谢，其所含的丙醇二酸能有效抑制糖类转变成脂肪。\n",
      "[('黄瓜', '含有', '维生素C')]\n",
      "**************************************************\n",
      "冠心病患者应该多吃蔬菜、水果。蔬菜、水果是维生素、矿物质、膳食纤维的很好来源，能降低人体对胆固醇的吸收，有利于降低血液黏稠度，提高血管通透性。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "豆腐皮中的蛋氨酸不仅是神经传导物质的合成原料，还能提高肝脏的解毒作用，强化肝脏功能。\n",
      "[]\n",
      "**************************************************\n",
      "竹荚鱼中富含名为EPA的不饱和脂肪酸，能减少人体血液中的胆固醇，还具有防止血栓的作用。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "维生素B2主要存在于动物性蛋白中，如蛋黄、肝、肾、瘦肉等；绿叶蔬菜、茶中含有丰富的叶酸，平时只要注意多吃动物蛋白和绿叶蔬菜，适当喝茶，就可以提供身体所需要的维生素B2和叶酸。\n",
      "[]\n",
      "**************************************************\n",
      "值得一提的是，蛳鱼背部中含有丰富的牛磺酸，能强化肝脏功能、缓解疲劳。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "竹荚鱼中丰富的DHA能促进大脑发育。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "这是因为这些食物每日可以进食较大的量，而肉类、鱼类虽然也可以提供钾，却不适合多吃，否则有引发肥胖、“三高”等疾病的隐患。\n",
      "[]\n",
      "**************************************************\n",
      "豆腐是豆类蛋白含量最高的食物，能够降低胆固醇，还能减少妇女更年期的潮热反应，同时使骨骼健肚。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "富含花青素的食物有蓝莓、葡萄、红紫苏、茄子等蔬果。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "胡萝卜叶中所含维生素A是根部的两倍，还含有大量的膳食纤维。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "贫血患者也要摄取青椒、西蓝花等蔬菜中丰富的叶酸\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "胡萝卜含有丰富的B-胡萝卜素，在体内能够转化为维生素A,能帮助保持眼睛的润滑及透明度，促进眼睛的健康。\n",
      "[('胡萝卜', '含有', 'B-胡萝卜素')]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "西瓜皮和红色果肉之间的白色部分含有大量瓜氨酸，它是氨基酸的一种。人体摄入后能消除身体水肿，促进血液流通，维持体温。建议吃完果肉后不要扔掉这部分，可以进行腌制或做成沙拉食用。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "冠心病患者需要避免饱和脂肪酸摄入过多。饱和脂肪酸是影响血脂的最主要因素，会导致血清总胆固醇和低密度脂蛋白胆固醇（坏胆固醇）水平的升高，要尽可能少地摄取饱和脂防酸(<总热量的10%)。同时，增加不饱和脂肪酸（鱼、禽类等）的摄入能降低胆固醇。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "明日叶的液汁含有查耳酮，能改善便秘。而且还具有抗氧化作用，能预防癌症。\n",
      "[('明日叶', '含有', '查耳酮')]\n",
      "**************************************************\n",
      "吃苹果时会尝到酸味，这是因为苹果中含有苹果酸和柠檬酸等有机酸，人体摄入后能消除疲劳。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "每天吃7颗中等大小的草莓就能满足1天必要的维生素C摄取量。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "燕麦富含可溶性膳食纤维，可促进肠胃蠕动，减少胆固醇的吸收。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "黄豆富含亚油酸、不饱和脂肪酸，均具有降低血液中胆固的作用，可减少动脉硬化的发生，预防高血压、冠心病等疾病。\n",
      "[('黄豆', '含', '亚油酸')]\n",
      "**************************************************\n",
      "**************************************************\n",
      "生吃青椒能摄取到比柠檬中含量更多的维生素C。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "牛奶和豆浆中的水分含量很高，还可以提供蛋白质、钙等营养成分，又能解饿。但是不能把牛奶和豆浆当水喝，否则容易摄入过多的脂肪，引发健康隐患\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "茄子富含芦丁，可增加毛细血管韧性和弹性，减小血管阻力，保证血液流通，避免血管破裂，进而降低血压。\n",
      "[('茄子', '含', '芦丁')]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "裙带菜中含丰富的海藻酸，可以给予肠道温和的刺激，促进排便。\n",
      "[('裙带菜', '含', '海藻酸')]\n",
      "**************************************************\n",
      "**************************************************\n",
      "猪肉中维生素B的含量是糙米的2倍，是人体重要的维生素B摄取来源。\n",
      "[]\n",
      "**************************************************\n",
      "备孕女性应该多吃富含叶酸的食物：橘子、橙子、柠檬、葡萄柚等、菠菜、西蓝花、芦笋、莴笋、油菜等、大豆及豆制品、花生(花生酱人葵花子等、大麦、米糠、小麦胚芽、糙米等、动物肝脏、牛奶及乳制品\n",
      "[]\n",
      "**************************************************\n",
      "烹调用油可以为人体提供能量，促进脂溶性维生素如维生素A、维生素D、维生素E等的吸收，但是摄入过多血管容易被淤积的油脂等堵住，引起血脂异常、动脉硬化等心血管疾病，还会导致肥胖、脂肪肝、糖尿病等慢性病。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "海带能促进三大营养素的代谢，增加身体活力，还能燃烧脂肪，促进儿童生长发育。\n",
      "[]\n",
      "**************************************************\n",
      "应该有谷类、豆制品类、奶类、蛋类、肉类、蔬菜、水果等，使食物蛋白质中的8种必需氨基酸组成比例趋于平衡，营养互补。\n",
      "[]\n",
      "**************************************************\n",
      "章鱼具有高蛋白而又低脂肪、低能量，因此不易被人体消化，有助于减肥。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "维生素A原的良好来源是富含B一胡萝卜素的黄绿色蔬菜和水果，如西蓝花、胡萝卜、红薯、茴香、荠菜、芒果等。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "熟吃能够充分吸收胡萝卜素，胡萝卜素是脂溶性物质，和番茄里的番茄红素一样，只有在烹调时适当放油，才会大大提高胡萝卜素的吸收率。\n",
      "[]\n",
      "**************************************************\n",
      "经常流鼻涕、鼻塞可以多食肉类、鱼类、蛋类等含有蛋白质的食物来增强体质\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "豆腐皮中富含钙和钾等矿物质以及维生素B1、维生素B2、维生素E,能促进消化和吸收。\n",
      "[]\n",
      "**************************************************\n",
      "B族维生素（豆类、绿叶蔬菜）可促进肠胃蠕动，增强肠道的紧张力，防止大便千结。\n",
      "[]\n",
      "**************************************************\n",
      "EPA、DHA存在于鱼油当中，在食用竹荚鱼时，建议选择煮汤或腌制的方法，能够将鱼油一同食用。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "鸡蛋富含人体所需的氨基酸，蛋黄富含卵磷脂，适合脑力工作者。\n",
      "[('鸡蛋', '含', '氨基酸')]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "海带外表覆盖着一层类似白霜的物质，那是重要的营养成分一甘露醇，该物质具有降压利尿和消肿的作用，在解酒、减肥方面也有一定的效果，在买干海带时应选择白霜多的。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "蓝莓中含有大量膳食纤维，能改善肠道环境。\n",
      "[('蓝莓', '含有', '膳食纤维')]\n",
      "**************************************************\n",
      "Ω-3脂肪酸对神经系统有保护作用，有助于健脑。研究表明，鱼类中富含Ω-3脂肪酸，吃鱼有助于加强神经细胞的活跃程度，从而提高学习和记忆能力。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "白菜中维生素和膳食纤维含量高，切的时候宜顺其纹理切，这样可减少维生素和膳食纤维的损失，并且相对易熟。烹调的时候加点醋，可减少白菜中维生素C的损失。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "维生素A可保护皮肤和黏膜\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "秋刀鱼脂肪中含有丰富的EPA,能降低胆固醇值预防血栓。特别是当季的秋刀鱼，营养价值和味道与其他时候相比有显著提升。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "葡萄柚中还富含柠檬酸，能够缓解疲劳。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "蘑菇中含有大量谷氨酸，是蘑菇中的香味成分。人体摄入后能集中注意力以及增强记忆力。\n",
      "[('蘑菇', '含有', '谷氨酸')]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "香菇富含硒元素，能有效清除体内的自由基，增强人体免疫功能。\n",
      "[]\n",
      "**************************************************\n",
      "虾中还包含甜菜碱，这一成分具有维持体温的作用。\n",
      "[('虾', '包含', '甜菜碱')]\n",
      "**************************************************\n",
      "不同部位脂肪含量不同。鸡胸脯肉的脂肪含量很低，而且含有大量维生素，鸡翅膀却含有较多脂肪，想减肥的人和血脂高的人应尽量少吃。\n",
      "[]\n",
      "**************************************************\n",
      "蛋白质和维生素C能有效缓解缺铁性贫血。\n",
      "[]\n",
      "**************************************************\n",
      "虽然胆固醇高的人要避免食用鸡蛋，但蛋类能保证体内卵磷脂与胆固醇值正常，因此胆固醇高的人也无须担心。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "毛豆中的胡萝卜素具有抗氧化作用，能防止身体衰老。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "心情急躁、紧张不安时推荐多食猪肉来补充维生素B1。\n",
      "[]\n",
      "**************************************************\n",
      "韭菜的香味来源于其中所含的硫化丙烯基。它能预防感冒，促进血液循环，维持体温\n",
      "[]\n",
      "**************************************************\n",
      "白萝卜宜分段食用：白萝卜从顶部至3~5厘米处为第一段，此段维生素C含量最多，宜切丝、条，快速烹调；和羊肉一起做馅，味道极佳。\n",
      "[]\n",
      "**************************************************\n",
      "除孕妇外，成年人、老年人不宜用骨头汤补钙。\n",
      "[]\n",
      "**************************************************\n",
      "眼睛疲劳推荐摄取维生素A和花青素\n",
      "[]\n",
      "**************************************************\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "谷类包含碳水化合物、膳食纤维、B族维生素、蛋白质、矿物质等营养物质\n",
      "[('谷类', '包含', '矿物质'),\n",
      " ('谷类', '包含', '膳食纤维'),\n",
      " ('谷类', '包含', 'B族维生素'),\n",
      " ('谷类', '包含', '蛋白质'),\n",
      " ('谷类', '包含', '碳水化合物')]\n",
      "**************************************************\n",
      "**************************************************\n",
      "高血压患者应该控制饮食中盐分的摄入，多摄取富含于蔬菜、水果及海藻等食物中的钾。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "因此，应多食用含铁质丰富的食物如猪肝、海带、鱼肉、牛肉等。\n",
      "[]\n",
      "**************************************************\n",
      "腹泻患者需要及时补充维生素C(如石榴、樱桃等)。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "多吃蔬菜和水果，有利于血脂异常患者。蔬菜和水果可提供丰富的维生素C、维生素E及胡萝卜素、番茄红素等天然抗氧化剂，可以防止坏胆固醇（低密度脂蛋白胆固醇)氧化、堆积，有利于降血脂。\n",
      "[]\n",
      "**************************************************\n",
      "少食多餐。将一日三餐改为5餐或6餐可避免一次饮食过多而导致血糖猛然升高，但加餐的食物也要计算在总热量里。牛奶、鸡蛋、豆腐干等蛋白质类食物，以及黄瓜、番茄、梨等低热量蔬果都是加餐的好选择。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "每天摄入300克奶或相当量的奶制品就能较好地补充钙，对于预防骨质疏松非常关键。奶类还是优质蛋白质和B族维生素的良好来源。\n",
      "[]\n",
      "**************************************************\n",
      "然而，各类粗杂粮在经过去皮、磨粉、过罗，添加饱和脂肪酸过多的动物油脂、糖甚至色素，再经过蒸煮甚至油炸之后，营养功效大打折扣。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "土豆富含淀粉、钾、维生素C、B族维生素，其中的淀粉生糖速度相对较慢，更抗饿。\n",
      "[]\n",
      "**************************************************\n",
      "紫色：富含美颜花青素，如茄子、紫甘蓝、洋葱等\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "橘子橙色果肉部分中含有β-胡萝卜素和β-隐黄素等抗氧化维生素。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "而且油菜中维生素C含量较多，摄入后能提高人体免疫力。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "青花鱼背部稍微发黑的肉中含有铁和EPA等促进血液循环的成分，应连同这部分鱼肉一起食用，能有效防止血栓和动脉硬化。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "花蛤中含有丰富的钙质能安定精神。同时还具有利尿作用，能消除身体水肿。\n",
      "[]\n",
      "**************************************************\n",
      "水果中丰富的维生素C和坚果中的维生素也可以有效缓解头痛\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "此外，薯类还普遍含有抗氧化成分，比如紫薯含花青素，山药和红薯等膳食纤维含量高，可预防便秘、肠癌。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "青花鱼中含有的牛磺酸能保护胃的健康，还具有增强体力的功效。\n",
      "[]\n",
      "**************************************************\n",
      "豆腐渣中还富含卵磷脂，能提高记忆力。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "鱼肉含有优质蛋白质，而且脂肪里含有大量的不饱和脂肪酸，如EPA、DHA,能够促进大脑发育、保护视力，对眼睛有益。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "为了控制脂肪的摄入量，应尽量选择含脂肪较少的食物。如选用瘦肉代替肥肉或五花肉，选用低脂的鱼、兔肉代替猪、羊肉；\n",
      "[]\n",
      "**************************************************\n",
      "柿子具有抗癌作用，富含维生素C\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "竹笋中含有不可溶性膳食纤维的纤维素，通过吸收肠道内的水分而变得膨胀，更好地排出肠道内的有害物质。\n",
      "[('竹笋', '含有', '不可溶性膳食纤维'), ('竹笋', '含有', '纤维素')]\n",
      "**************************************************\n",
      "**************************************************\n",
      "在海藻类中，羊栖菜的铁和钙含量都是最高的。\n",
      "[]\n",
      "**************************************************\n",
      "在肉类当中，牛肉的铁含量较为丰富，食用牛肉能改善贫血。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "素食人群，黄豆及其豆制品是优质蛋白质的主要来源。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n",
      "**************************************************\n",
      "番茄所含的苹果酸、柠檬酸等有机酸，能促使胃液分泌，增加胃酸浓度，调整胃肠功能，有助于胃肠疾病的康复。\n",
      "[]\n",
      "**************************************************\n",
      "造成洋葱特殊的辣味以及切洋葱时引起流泪的原因是洋葱中含有一种硫化丙烯基的成分。\n",
      "[]\n",
      "**************************************************\n",
      "**************************************************\n"
     ]
    }
   ],
   "source": [
    "def create_words(label):\n",
    "    words = []\n",
    "    disease_words = []\n",
    "    nutrient_words = []\n",
    "    crowd_words = []\n",
    "    food_words = []\n",
    "    organ_words = []\n",
    "    for key in list(label.keys()):\n",
    "        if key == 'food':\n",
    "            food_words = [word for word in label[key]]\n",
    "        elif key == 'disease':\n",
    "            disease_words = [word for word in label[key]]\n",
    "        elif key == 'nutrient':\n",
    "            nutrient_words = [word for word in label[key]]\n",
    "        elif key == 'crowd':\n",
    "            crowd_words = [word for word in label[key]]\n",
    "        elif key == 'organ':\n",
    "            organ_words = [word for word in label[key]]\n",
    "        for word in label[key]:\n",
    "            words.append(word)\n",
    "    return (words, disease_words, nutrient_words, crowd_words, food_words, organ_words)\n",
    "\n",
    "def extract_one_sentence(data, ltp):\n",
    "    text = data['text']\n",
    "    label = data['label']\n",
    "    words, disease_words, nutrient_words, crowd_words, food_words, organ_words = create_words(label)\n",
    "    ltp.add_words(words)\n",
    "\n",
    "    result = ltp.pipeline([text], tasks = [\"cws\",\"dep\"])\n",
    "    parsed_dep = parse_dep(result.cws[0], result.dep[0]['head'], result.dep[0]['label'])\n",
    "\n",
    "    result = []\n",
    "    if food_words and nutrient_words:\n",
    "        print(text)\n",
    "        pprint(get_food_to_nutient(parsed_dep, food_words, nutrient_words))\n",
    "    return result\n",
    "\n",
    "ltp = LTP(\"/Users/lazyben/Downloads/small\")\n",
    "with open(r'./train.json', 'r') as f:\n",
    "    for i in f.readlines()[:300]:\n",
    "        data = eval(i)\n",
    "        extract_one_sentence(data, ltp)\n",
    "        print('*'*50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_food_to_nutient(parsed_dep, food_words, nuritent_words):\n",
    "    ## 确定需要的root\n",
    "    root = parsed_dep[parsed_dep[0]['to'][0]['idx']]\n",
    "    if '含' not in root['name']:\n",
    "        for to in root['to']:\n",
    "            if '含' in to['name'] and to['label']=='COO':\n",
    "                root = parsed_dep[to['idx']]\n",
    "                break\n",
    "    if '含' not in root['name']:\n",
    "        return []\n",
    "    food = get_root_to_food(parsed_dep, food_words, root)\n",
    "    nutrient = get_root_to_nutrient(parsed_dep, nuritent_words, root)\n",
    "    result = []\n",
    "    if food and nutrient:\n",
    "        for i in nutrient:\n",
    "            result.append((food, root['name'], i))\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "## root to food\n",
    "def get_root_to_food(parsed_dep, food_words, root):\n",
    "    root = parsed_dep[parsed_dep[0]['to'][0]['idx']]\n",
    "    for ele in root['to']:\n",
    "        if ele['label'] == 'SBV':\n",
    "            if ele['name'] in food_words:\n",
    "                return ele['name']\n",
    "            else:\n",
    "                sbv = parsed_dep[ele['idx']]\n",
    "                for to in sbv['to']:\n",
    "                    if to['label'] == \"ATT\" and to['name'] in food_words:\n",
    "                        return to['name']\n",
    "    return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "## root to nutrient\n",
    "def get_root_to_nutrient(parsed_dep, nuritent_words, root):\n",
    "    VOB_ = []\n",
    "    nuritent = []\n",
    "    root = parsed_dep[parsed_dep[0]['to'][0]['idx']]\n",
    "    for ele in root['to']:\n",
    "        if ele['label'] == 'VOB':\n",
    "            if ele['name'] in nuritent_words:\n",
    "                nuritent.append(ele['name'])\n",
    "                cur_nu = parsed_dep[ele['idx']]\n",
    "                for nu in cur_nu['to']:\n",
    "                    if nu['name'] in nuritent_words:\n",
    "                        nuritent.append(nu['name'])\n",
    "            else:\n",
    "                VOB_.append(parsed_dep[ele['idx']])\n",
    "    for vob in VOB_:\n",
    "        for to in vob['to']:\n",
    "            if to['label'] == \"ATT\" and to['name'] in nuritent_words:\n",
    "                nuritent.append(to['name'])\n",
    "                cur_nu = parsed_dep[to['idx']]\n",
    "                for nu in cur_nu['to']:\n",
    "                    if nu['name'] in nuritent_words:\n",
    "                        nuritent.append(nu['name'])\n",
    "    return list(set(nuritent))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
